using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.CommonWeb;
using NTTHAO.Web.BL.Panel;
using NTTHAO.Imaging;

public partial class admin_webparts_Plug_ins_Panel_PanelInput : System.Web.UI.UserControl
{
    public NTTHAO.Web.BL.Panel.Panel _Detail = null;
    public NTTHAO.Web.BL.Panel.PanelGroup _Group = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.btnSave.Text = this.btnSave2.Text = LanguageUtility.GetLanguageText("SAVE", Session);
        this.chkActive.Text = LanguageUtility.GetLanguageText("ACTIVE", Session);
        if (!IsPostBack)
        {
            try
            {
                _Group = new PanelGroup(Int32.Parse(Request.QueryString["groupid"]));

                if ("imageedit".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
                {
                    int imageID = Int32.Parse(Request.QueryString["imageid"]);

                    NTTHAO.Web.BL.Panel.Panel panel = new NTTHAO.Web.BL.Panel.Panel(imageID);
                    if (panel != null)
                    {
                        this._Detail = panel;

                        this.txtTitle.Text = panel.Title;
                        this.txtURL.Text = panel.Link.Replace("http://", "");
                        this.chkActive.Checked = panel.Actived;
                    }
                    else
                    {
                        Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=viewgroup&groupid=" + Request.QueryString["groupid"], false);
                    }
                }
            }
            catch (Exception)
            {
                Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=viewgroup&groupid=" + Request.QueryString["groupid"], false);
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            PanelGroup group = new PanelGroup(Int32.Parse(Request.QueryString["groupid"].Trim()));
            NTTHAO.Web.BL.Panel.Panel panel = null;

            if ("imageedit".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                panel = new NTTHAO.Web.BL.Panel.Panel(Int32.Parse(Request.QueryString["imageid"]));
            }
            else
            {
                panel = new NTTHAO.Web.BL.Panel.Panel();
                panel.GroupID = group.ID;
                panel.Reserve_01 = "";
            }

            panel.Title = txtTitle.Text;
            string url = txtURL.Text.Trim();
            url = (url.Length > 0) ? url : "#";
            url = (url.StartsWith("/") || url.StartsWith("#")) ? url : ("http://" + url.Replace("http://", ""));
            panel.Link = url;
            panel.Actived = chkActive.Checked;

            if ("imageedit".Equals(Request.QueryString["action"], StringComparison.OrdinalIgnoreCase))
            {
                panel.Update();
            }
            else
            {
                panel.FileName = "";
                panel.FileType = "";
                panel.Insert();
            }

            if (this.fuImage.HasFile)
            {
                this.SavePostedFile(group, ref panel, this.fuImage);

                panel.Update();
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message + "<br /><br />" + ex.StackTrace);
            return;
        }
        Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=viewgroup&groupid=" + Request.QueryString["groupid"], false);
    }

    public string SavePostedFile(PanelGroup group, ref NTTHAO.Web.BL.Panel.Panel panel, FileUpload uploader)
    {
        string filePath = "../" + Constant.UPLOAD_FILE_PATH + "panel/";
        string imagePath = filePath + group.ID.ToString() + "/";
        if (!System.IO.Directory.Exists(Server.MapPath(imagePath)))
        {
            System.IO.Directory.CreateDirectory(Server.MapPath(imagePath));
        }

        if (System.IO.File.Exists(Server.MapPath(imagePath + panel.FileName)))
        {
            System.IO.File.Delete(Server.MapPath(imagePath + panel.FileName));
        }

        panel.FileType = Path.GetExtension(uploader.PostedFile.FileName);
        panel.FileName = "Image_" + panel.ID.ToString() + "_" + DateTime.Now.Ticks.ToString() + panel.FileType;

        uploader.PostedFile.SaveAs(Server.MapPath(imagePath + panel.FileName));

        string miniFilePath = Path.Combine(imagePath, "Mini_" + panel.FileName);
        if (File.Exists(miniFilePath))
        {
            File.Delete(miniFilePath);
        }
        File.Copy(Server.MapPath(imagePath + panel.FileName), Server.MapPath(miniFilePath));
        ImageUtilities.MaximumSizeFit(Server.MapPath(miniFilePath), 150, 70, ImageUtilities.STANDARD_PROGRESSIVE);

        return panel.FileName;
    }
}
